/*
 *  $Id: Parasite.cs 1189 2010-11-12 14:40:06Z thenn.erannor $
 *	This file is a part of Ragima CCG.
 *	(C) 2008 Ragima development group.
 *
 *	Licensed under the terms of the GNU GPL License version 2.
 *
 */

using Ragima.Helper;

namespace Ragima.Affects {
    /// <summary>
    /// Эффект: паразит
    /// Параметры: значение (сила паразита)
    /// В начале хода уменьшает здоровье и атаку существа на силу паразита.
    /// Если носителя паразита убивает другое существо, оно тоже получает паразита.
    /// </summary>
    public class Parasite: Affect {
        public readonly int Value;

        public Parasite(int value) {
            Value = value;
        }

        public override string Id {
            get { return "parasite"; }
        }
        public override string NameText {
            get { return Name.GetText(Value); }
        }

        public override int BaseWeight {
            get { return Value * -60; }
        }

    	public override AffectIcon Icon {
            get { return AffectIcon.Parasite; }
        }

        public override Affect Clone() {
            return new Parasite(Value);
        }

        public override void SetAffect() {
            base.SetAffect();
            LocalSummary.OnStartOffencePhase += DoParasite;
            LocalSummary.OnAfterDamage += Damage;
        }
        public override void ResetAffect() {
            base.ResetAffect();
            LocalSummary.OnStartOffencePhase -= DoParasite;
            LocalSummary.OnAfterDamage -= Damage;
        }

        private void DoParasite() {
            if (Owner.HasFlag<Undead>())
                return;
            Region region = Owner.FindRegion();
			UI.ParticleInfo info = new UI.ParticleInfo(Data.Particles.Cloud01);
            info.AddPhase(0, 0, 0xBBc849.ToColor(), 0.1, 0.4, 0, UI.ParticleTrajectory.Line(-90, 0.6));
            info.AddPhase(30, 40, 0x901212.ToColor(), 0.5, 0.65);
            info.AddPhase(60, 0, 0xBBc849.ToColor(), 0.1);
            UI.ParticleEmitterInfo emitter = new UI.ParticleEmitterInfo();
            emitter.Duration = 70;
            emitter.Rate = UI.ParticleRate.Always(10);
            emitter.Generator = UI.ParticleGenerator.Same(info, UI.ParticlePoints.EngulfFromBottom(region.Rect, 2));
			Application.CurrentScene.AddEmitter(emitter.Create());
            LocalSummary.ChangeDamage(-Value);
            LocalSummary.ChangeMaxHealth(-Value);
        }

        private void Damage(AbstractObject target, DamageInfo damage) {
            Creature attacker;
            if (MeleeDamageInfo.TryGetAttacker(damage, out attacker) && target.IsDeath) {
                Region region1 = attacker.FindRegion();
                Region region2 = Owner.FindRegion();
				UI.ParticleInfo info = new UI.ParticleInfo(Data.Particles.Cloud01);
				info.AddPhase(0, 0, 0xBBc849.ToColor(), 0.1, 0.4, 0, UI.ParticleTrajectory.FromTo(region1.Rect, region2.Rect, 1.5));
				info.AddPhase(30, 40, 0x901212.ToColor(), 0.5, 0.65);
				info.AddPhase(45, 0, 0xBBc849.ToColor(), 0.1);
                UI.ParticleEmitterInfo emitter = new UI.ParticleEmitterInfo();
                emitter.Duration = 50;
                emitter.Rate = UI.ParticleRate.Always(30);
                emitter.Generator = UI.ParticleGenerator.Same(info, UI.ParticlePoints.EngulfFromSide(region2.Rect,2,(region2.Rect.Y<region1.Rect.Y)?2:1));
				Application.CurrentScene.AddEmitter(emitter.Create());
                attacker.AddAffect(this.Clone());
			}
        }
    }

    /// <summary>
    /// Заразная атака
    /// Параметры: сила паразита
    /// Существо при ударе заражает свою цель паразитом указанной силы.
    /// </summary>
    public class ParasiteAttack: Affect {
        public readonly int Value;

        public ParasiteAttack(int value) {
            Value = value;
        }

        public override string Id {
            get { return "parasite.attack"; }
        }
        public override string NameText {
            get { return Name.GetText(Value); }
        }

        public override ElementType Element {
            get { return ElementType.Neutral; }
        }

        public override int BaseWeight {
            get { return Value * 60; }
        }

        public override AffectIcon Icon {
            get { return AffectIcon.ParasiteAttack; }
        }

        public override Affect Clone() {
            return new ParasiteAttack(Value);
        }
 
        public override void SetAffect() {
            base.SetAffect();
            LocalSummary.OnHit += DoHit;
        }
        public override void ResetAffect() {
            base.ResetAffect();
            LocalSummary.OnHit -= DoHit;
        }

        private void DoHit(Creature hitter, AbstractObject target, int damage) {
            if (target is Creature) target.AddAffect(new Parasite(Value));
        }
    }
}